package com.agricultural.management;

import com.agricultural.environment.EnvironmentMonitor;
import com.agricultural.equipment.AgriculturalEquipment;
import com.agricultural.factory.EquipmentFactory;
import com.agricultural.handler.SafetyHandler;
import com.agricultural.handler.impl.ObstacleDetectionHandler;
import com.agricultural.handler.impl.FuelLevelHandler;
import com.agricultural.handler.impl.SystemErrorHandler;
import com.agricultural.observer.Observer;

import java.util.HashMap;
import java.util.Map;

// 云端管理平台
public class CloudPlatform {
    private Map<String, AgriculturalEquipment> equipmentMap; // 设备映射表
    private EnvironmentMonitor environmentMonitor; // 环境监测器
    private SafetyHandler safetyHandlerChain; // 安全处理责任链
    
    // 构造函数
    public CloudPlatform() {
        this.equipmentMap = new HashMap<>();
        this.environmentMonitor = new EnvironmentMonitor();
        initializeSafetyHandlerChain();
    }
    
    // 初始化安全处理责任链
    private void initializeSafetyHandlerChain() {
        // 创建各个安全处理器
        SafetyHandler obstacleHandler = new ObstacleDetectionHandler();
        SafetyHandler fuelHandler = new FuelLevelHandler();
        SafetyHandler errorHandler = new SystemErrorHandler();
        
        // 设置责任链顺序
        obstacleHandler.setNextHandler(fuelHandler);
        fuelHandler.setNextHandler(errorHandler);
        
        // 设置链头
        this.safetyHandlerChain = obstacleHandler;
    }
    
    // 添加设备
    public void addEquipment(String type, String id) {
        // 使用工厂模式创建设备
        AgriculturalEquipment equipment = EquipmentFactory.createEquipment(type, id);
        
        // 注册设备为环境变化的观察者
        environmentMonitor.registerObserver((Observer) equipment);
        
        // 将设备添加到设备映射表中
        equipmentMap.put(id, equipment);
        
        System.out.println("添加设备: " + type + "-" + id);
    }
    
    // 获取设备
    public AgriculturalEquipment getEquipment(String id) {
        return equipmentMap.get(id);
    }
    
    // 显示所有设备状态
    public void showAllEquipmentStatus() {
        System.out.println("\n===== 设备状态 =====");
        for (Map.Entry<String, AgriculturalEquipment> entry : equipmentMap.entrySet()) {
            AgriculturalEquipment equipment = entry.getValue();
            System.out.println("设备ID: " + equipment.getId());
            System.out.println("设备类型: " + equipment.getType());
            System.out.println("当前位置: " + equipment.getStatus().get("position"));
            System.out.println("燃油水平: " + equipment.getStatus().get("fuelLevel") + "%");
            System.out.println("当前状态: " + equipment.getStatus().get("state"));
            System.out.println("-------------------------");
        }
    }
    
    // 更新环境数据
    public void updateEnvironmentData(String parameter, Object value) {
        environmentMonitor.updateEnvironmentData(parameter, value);
    }
    
    // 处理安全事件
    public void handleSafetyEvent(String equipmentId, String eventType, double parameter) {
        AgriculturalEquipment equipment = equipmentMap.get(equipmentId);
        if (equipment != null) {
            System.out.println("\n处理安全事件: " + eventType + "，设备: " + equipmentId);
            safetyHandlerChain.handleSafetyEvent(equipment, eventType, parameter);
        } else {
            System.out.println("错误: 未找到设备 " + equipmentId);
        }
    }
}    